package com.huangpuweb.api.service.imp;

import com.huangpuweb.api.model.*;
import com.huangpuweb.api.service.BlmqApiService;
import com.huangpuweb.api.util.StringUtil;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class BlmqApiServiceImp extends BaseBlmqDataServiceImp implements BlmqApiService {


    @Override
    public Integer getBlmqAllCount() {
        String sql="select count(1) from build_info where USE_NATURE is not null";
        return queryInteger(sql,new Object[]{});
    }


//    @Cacheable(value="onehour",key="getBlmqGroupUseNature")
    @Override
    public List<SysHpXiaoFangAjType> getBlmqGroupUseNature() {
        String sql="select any_value(USE_NATURE) as name,count(1) as count from build_info where USE_NATURE is not null and USE_NATURE !='' group by USE_NATURE order by count desc";
        return getEntryList(sql,new SysHpXiaoFangAjTypeRowMapper(),new Object[]{});
    }

    @Override
    public List<BlmqBuildInfoLevel> getListBlmqBuildInfoLevel() {
        String sql="select any_value(VILLAGE) as VILLAGE , any_value(BUILDING_SORT) as BUILDING_SORT from build_info where BUILDING_SORT is not null";
        return getEntryList(sql,new BlmqBuildInfoLevelRowMapper(),new Object[]{});
    }

    @Override
    public List<String> getListStreet() {
        String sql="select any_value(VILLAGE) as VILLAGE from build_info group by VILLAGE";
        return queryStringArray(sql,new Object[]{});
    }

    @Override
    public List<SysHpXiaoFangAjType> getListBlmqStreetLevel(String street) {
        String sql="select any_value(BUILDING_SORT) as `name`,count(1) as count from build_info where  VILLAGE=? and BUILDING_SORT is not NULL group by BUILDING_SORT";
        return getEntryList(sql,new SysHpXiaoFangAjTypeRowMapper(),new Object[]{street});
    }

    @Override
    public Integer getBlmqHeightAllCount() {
        String sql="select count(1) from build_info where WALL_HIGH is not null";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public List<Integer> getListBlmqHeight() {
        String sql="select WALL_HIGH from build_info where  WALL_HIGH is not NULL";
        return queryIntegerArray(sql,new Object[]{});
    }

    @Override
    public Integer getBlmqSizeAllCount() {
        String sql="select count(1) from build_info where WALL_AREA is not null";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public List<Integer> getListBlmqSize() {
        String sql="select WALL_AREA from build_info where  WALL_AREA is not NULL";
        return queryIntegerArray(sql,new Object[]{});
    }

    @Override
    public Integer getBlmqGlassType(String type) {
        String sql="select count(1) from build_info where GLASS_TYPE  like ?";
        return queryInteger(sql,new Object[]{"%" + type + "%"});
    }

    @Override
    public Integer getBlmqGlassSTRUCTURE(String type) {
        String sql="";
        if(StringUtil.isEmpty(type)){
            sql="select count(1) from build_info where STRUCTURE =''";
            return queryInteger(sql,new Object[]{});
        }else{
            sql="select count(1) from build_info where STRUCTURE  like ?";
            return queryInteger(sql,new Object[]{"%" + type + "%"});
        }


    }

    @Override
    public BlmqCountryCheck getBlmqCountryCheck(String year) {
        String sql="select * from county_check_count where check_year=? order by CREATE_TIME desc limit 1";
        return getEntry(sql,new BlmqCountryCheckRowMapper(),new Object[]{year});
    }

    @Override
    public Integer getQuHasCheckCount() {
        String sql="select count(1) from check_list where CHECK_STATUS=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getQuHasChangeCount() {
        String sql="select count(1) from check_list where NEED_CHANGE=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getOwnHasSubmitCount() {
        String sql="select count(1) from ownwe_check_list";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getOwnHasErrorCount() {
        String sql="select count(1) from ownwe_check_list where OPERABLE_VENT=1 OR  FORCE_MEMBER=1 OR  SEALANT=1 OR GLASS=1 OR RAIN=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public List<String> getListStreetBuileId(String street) {
        String sql="select BUILD_ID from build_info where VILLAGE=?";
        return queryStringArray(sql,new Object[]{street});
    }

    @Override
    public Integer getCheckStatusByBuildId(String st) {
        String sql="select CHECK_STATUS from check_list where BUILD_ID=?";
        return queryInteger(sql,new Object[]{st});
    }

    @Override
    public Integer getNeedChangeByBuildId(String st) {
        String sql="select NEED_CHANGE from check_list where BUILD_ID=?";
        return queryInteger(sql,new Object[]{st});
    }

    @Override
    public List<SysHpXiaoFangAjType> getListBlmqCheckErrorType() {
        String sql="select any_value(DAMAGE_TYPE) as `name`,count(1) as count from check_list_dtl  group by DAMAGE_TYPE";
        return getEntryList(sql,new SysHpXiaoFangAjTypeRowMapper(),new Object[]{});
    }

    @Override
    public Integer getOperableVentCount() {
        String sql="select count(1) from ownwe_check_list where OPERABLE_VENT=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getSEALANTCount() {
        String sql="select count(1) from ownwe_check_list where SEALANT=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getGLASSCount() {
        String sql="select count(1) from ownwe_check_list where GLASS=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getFORCE_MEMBERCount() {
        String sql="select count(1) from ownwe_check_list where FORCE_MEMBER=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getRainCount() {
        String sql="select count(1) from ownwe_check_list where RAIN=1";
        return queryInteger(sql,new Object[]{});
    }

    @Override
    public BlmqBuildInfo getBuildInfoByBuildId(String buildid) {
        String sql="select * from build_info where BUILD_ID=?";
        return getEntry(sql,new BlmqBuildInfoRowMapper(),new Object[]{buildid});
    }

    @Override
    public List<BlmqCheckListLog> getListBlmqCheckListLog() {
        String sql="select * from check_list_log order by FIND_TIME desc limit 3";
        return getEntryList(sql,new BlmqCheckListLogRowMapper(),new Object[]{});
    }

    @Override
    public BlmqCheckList getBlmqCheckListByBuildId(String buildid) {
        String sql="select * from check_list where BUILD_ID=?";
        return getEntry(sql,new BlmqCheckListRowMapper(),new Object[]{buildid});
    }

    @Override
    public  List<BlmqCheckListDtl> getBlmqCHeckListDtlByCheckListId(String id) {
        String sql="select * from check_list_dtl where CHECK_LIST_ID=?";
        return getEntryList(sql,new BlmqCheckListDtlRowMapper(),new Object[]{id});
    }

    @Override
    public BlmqCheckList getBlmqCheckListById(String check_lise_id) {
        String sql="select * from check_list where ID=?";
        return getEntry(sql,new BlmqCheckListRowMapper(),new Object[]{check_lise_id});
    }

    @Override
    public List<BlmqCheckListLog> getListBlmqCheckListLogById(String id) {
        String sql="select * from check_list_log where CHECK_LIST_ID=?";
        return getEntryList(sql,new BlmqCheckListLogRowMapper(),new Object[]{id});
    }

    @Override
    public List<Integer> getListBuildidBlmqSize(String type) {

        //热词 10000 5000  1000 500
        if(type.equals("10000")){
            String sql="select BUILD_ID from build_info where  WALL_AREA>=?";
            return queryIntegerArray(sql,new Object[]{10000});
        }

        if(type.equals("5000")){
            String sql="select BUILD_ID from build_info where  WALL_AREA>=? and  WALL_AREA<?";
            return queryIntegerArray(sql,new Object[]{5000,10000});
        }


        if(type.equals("1000")){
            String sql="select BUILD_ID from build_info where  WALL_AREA>=? and  WALL_AREA<?";
            return queryIntegerArray(sql,new Object[]{1000,5000});
        }


        if(type.equals("500")){
            String sql="select BUILD_ID from build_info where  WALL_AREA<?";
            return queryIntegerArray(sql,new Object[]{1000});
        }
        return null;
    }

    @Override
    public List<Integer> getListBuildidBlmqBlmqGlassType(String type) {
        String sql="select BUILD_ID from build_info where GLASS_TYPE  like ?";
        return queryIntegerArray(sql,new Object[]{"%" + type + "%"});
    }

    @Override
    public List<Integer> getBlmqGlassSTRUCTUREByType(String type) {
        String sql="";
        if(type.equals("其他")){
            sql="select BUILD_ID from build_info where STRUCTURE =''";
            return queryIntegerArray(sql,new Object[]{});
        }else{
            sql="select BUILD_ID from build_info where STRUCTURE  like ?";
            return queryIntegerArray(sql,new Object[]{"%" + type + "%"});
        }
    }

    @Override
    public List<BlmqCheckList> getListBlmqCheckList() {
        String sql="select id,build_id,need_change,high_deop_risk,check_time from check_list   order by CHECK_TIME desc ";
        return getEntryList(sql, new RowMapper<BlmqCheckList>() {
            @Override
            public BlmqCheckList mapRow(ResultSet resultSet, int i) throws SQLException {
                BlmqCheckList blmqCheckList=new BlmqCheckList();
                blmqCheckList.setID(resultSet.getString("ID"));
                blmqCheckList.setBUILD_ID(resultSet.getInt("build_id"));
                blmqCheckList.setNEED_CHANGE(resultSet.getInt("need_change"));
                blmqCheckList.setHIGH_DEOP_RISK(resultSet.getInt("HIGH_DEOP_RISK"));
                blmqCheckList.setCHECK_TIME(resultSet.getString("CHECK_TIME"));
                return blmqCheckList;
            }
        }, new Object[]{});
    }

        @Override
    public BlmqCheckListLog getBlmqCheckListLogById(String id) {
        String sql="select * from check_list_log where CHECK_LIST_ID=? order by LAST_DATE desc limit 1";
        return getEntry(sql,new BlmqCheckListLogRowMapper(),new Object[]{id});
    }

}
